package com.cy.algorithm.binarytree.cover;

/**
 * 递归遍历二叉树
 *
 * @author chen_yu
 * @date 2020年12月13日20:17:57
 * A
 * B  C
 * D   E  F
 */
public class RecursiveBinaryTree {

    public static void main(String[] args) {
        TreeNode<String> node =
                new TreeNode<>("A",
                        new TreeNode<>("B", new TreeNode<>("D", new TreeNode<>("G"), new TreeNode<>("H"))),
                        new TreeNode<>("C", new TreeNode<>("E", null, new TreeNode<>("I")), new TreeNode<>("F"))
                );

        preOrder(node);
        System.out.println();
        midOrder(node);
        System.out.println();
        afterOrder(node);
    }


    /**
     * 前序遍历
     *
     * @param node 根节点
     * @param <T>  泛型T
     */
    public static <T> void preOrder(TreeNode<T> node) {
        if (node == null) {
            return;
        }
        System.out.print(node.getData() + "->");
        preOrder(node.getLeft());
        preOrder(node.getRight());
    }

    /**
     * 中序遍历
     *
     * @param node 根节点
     * @param <T>  泛型T
     */
    public static <T> void midOrder(TreeNode<T> node) {
        if (node == null) {
            return;
        }
        midOrder(node.getLeft());
        System.out.print(node.getData() + "->");
        midOrder(node.getRight());
    }

    /**
     * 后序遍历
     *
     * @param node 根节点
     * @param <T>  泛型T
     */
    public static <T> void afterOrder(TreeNode<T> node) {
        if (node == null) {
            return;
        }
        afterOrder(node.getLeft());
        afterOrder(node.getRight());
        System.out.print(node.getData() + "->");
    }
}
